{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T13:15:09.668253Z",
     "start_time": "2024-09-23T13:15:09.655920Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据处理、数据评分相关库\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "# 画图\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "%matplotlib inline\n",
    "plt.rcParams['font.sans-serif']='SimHei'# 设置中文显示\n",
    "plt.rcParams['font.size']=14 # 设置字体大小\n",
    "matplotlib.rcParams['axes.unicode_minus'] = False # 解决负号问题\n",
    "\n",
    "#忽略警号\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ],
   "id": "b78e2526a17ca23b",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "切分数据",
   "id": "73f0fdcba39afcc4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:22.466201Z",
     "start_time": "2024-09-23T00:55:21.102518Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.read_csv('data.csv')\n",
    "data.fillna(0,inplace=True)"
   ],
   "id": "cba4f1ca205f3b15",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:22.629633Z",
     "start_time": "2024-09-23T00:55:22.467198Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train = data[data['origin']=='train'].drop(['origin'],axis=1)\n",
    "test = data[data['origin']=='test'].drop(['label','origin'],axis=1)"
   ],
   "id": "f69bebac67137600",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:22.655036Z",
     "start_time": "2024-09-23T00:55:22.630598Z"
    }
   },
   "cell_type": "code",
   "source": "X,Y = train.drop(['label'],axis=1),train['label'] ",
   "id": "3f0ec240813dc4ee",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:22.867393Z",
     "start_time": "2024-09-23T00:55:22.656028Z"
    }
   },
   "cell_type": "code",
   "source": "from sklearn.model_selection import train_test_split",
   "id": "3213742f3d33273d",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "建模",
   "id": "a9b1c99d66cef522"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:22.972949Z",
     "start_time": "2024-09-23T00:55:22.871193Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "from sklearn.feature_selection import mutual_info_classif\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.metrics import roc_auc_score, roc_curve, accuracy_score, confusion_matrix, log_loss, auc, \\\n",
    "    precision_recall_curve\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n"
   ],
   "id": "be416f57116b58d3",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:22.978909Z",
     "start_time": "2024-09-23T00:55:22.974937Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def model_clf(model):\n",
    "    model.fit(train_x, train_y)\n",
    "    y_train_pred = model.predict_proba(train_x)\n",
    "    y_train_pred_pos = y_train_pred[:,1]\n",
    "\n",
    "    y_test_pred = model.predict_proba(valid_x)\n",
    "    y_test_pred_pos = y_test_pred[:,1]\n",
    "# 计算训练数据的AUC（曲线下面积）评分\n",
    "    auc_train = roc_auc_score(train_y, y_train_pred_pos)#AUC评分\n",
    "    # 计算测试数据的AUC评分\n",
    "    auc_test = roc_auc_score(valid_y, y_test_pred_pos)\n",
    "\n",
    "    print(f\"Train AUC Score {auc_train}\")\n",
    "    print(f\"Test AUC Score {auc_test}\")\n",
    "\n",
    "    fpr, tpr, _ = roc_curve(valid_y,y_test_pred_pos)#绘制ROC曲线\n",
    "    return fpr,tpr"
   ],
   "id": "420b0fbab2a92a8f",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T00:55:23.060778Z",
     "start_time": "2024-09-23T00:55:22.979890Z"
    }
   },
   "cell_type": "code",
   "source": "train_x,valid_x,train_y,valid_y = train_test_split(X,Y,test_size=0.2)\n",
   "id": "5fb2f447b72d3f58",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T11:33:07.808590Z",
     "start_time": "2024-09-23T11:33:00.758770Z"
    }
   },
   "cell_type": "code",
   "source": [
    "clf = RandomForestClassifier(\n",
    "    n_estimators=50,  # 减少树的数量\n",
    "    max_depth=5,     # 限制树的最大深度\n",
    "    min_samples_split=5,  # 增加分裂所需的最小样本数\n",
    "    min_samples_leaf=2,    # 增加叶子节点所需的最小样本数\n",
    ")\n",
    "model_clf(clf)"
   ],
   "id": "4067a8bf1b9d121f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train AUC Score 0.6478755740885015\n",
      "Test AUC Score 0.6442764370072948\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([0.00000000e+00, 2.04240023e-05, 4.08480046e-05, ...,\n",
       "        9.99673216e-01, 9.99734488e-01, 1.00000000e+00]),\n",
       " array([0., 0., 0., ..., 1., 1., 1.]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T02:10:08.384593Z",
     "start_time": "2024-09-23T00:55:30.298347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, Y, random_state=0)\n",
    "clf = RandomForestClassifier(n_estimators=10, max_depth=3, min_samples_split=12, random_state=0)\n",
    "clf.fit(X_train, y_train)\n",
    "#网格搜索\n",
    "clf_params = {\"n_estimators\":[20,50,100],  \n",
    "          \"max_depth\":[5,10,100],\n",
    "          \"min_samples_split\":[2,10,500],\n",
    "          \"min_samples_leaf\":[1,50,100]\n",
    "         }\n",
    "\n",
    "clf_model=GridSearchCV(clf, param_grid=clf_params, cv=5)\n",
    "clf_model.fit(X_train, y_train)\n",
    "#获取最优参数\n",
    "clf_best_model=clf_model.best_estimator_\n",
    "\n",
    "print(f\"参数 {clf_best_model}\")\n",
    "\n",
    "y_train_pred = clf_best_model.predict_proba(train_x)\n",
    "y_train_pred_pos = y_train_pred[:,1]\n",
    "\n",
    "y_test_pred = clf_best_model.predict_proba(valid_x)\n",
    "y_test_pred_pos = y_test_pred[:,1]\n",
    "# 计算训练数据的AUC（曲线下面积）评分\n",
    "auc_train = roc_auc_score(train_y, y_train_pred_pos)#AUC评分\n",
    "# 计算测试数据的AUC评分\n",
    "auc_test = roc_auc_score(valid_y, y_test_pred_pos)\n",
    "\n",
    "print(f\"Train AUC Score {auc_train}\")\n",
    "print(f\"Test AUC Score {auc_test}\")"
   ],
   "id": "271ef19000492cde",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "参数 RandomForestClassifier(max_depth=5, n_estimators=20, random_state=0)\n",
      "Train AUC Score 0.6457804039771786\n",
      "Test AUC Score 0.6497014966233101\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-23T12:00:30.420071Z",
     "start_time": "2024-09-23T12:00:30.168339Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取预测概率\n",
    "probabilities = clf_best_model.predict_proba(X_test)[:, 1]\n",
    "#  \n",
    "#  获取测试集在原始数据中的索引\n",
    "test_indices =  X_test.index  # 假设 X_test 是一个可获取索引的对象，如果不是，需要根据具体情况调整\n",
    "\n",
    "# 创建包含预测结果的 DataFrame\n",
    "result_df = pd.DataFrame({\n",
    "    'user_id': data.iloc[test_indices]['user_id'],\n",
    "    'merchant_id': data.iloc[test_indices]['merchant_id'],\n",
    "    'prob': probabilities\n",
    "})\n",
    "\n",
    "b = pd.DataFrame(probabilities).reset_index(drop=True)\n",
    "\n",
    "a = data[['user_id','merchant_id']]\n",
    "# 将结果保存为 CSV 文件\n",
    "# result_df.to_csv('C:\\\\Users\\\\22311\\\\Desktop\\\\longdan1.csv',index=False)\n",
    "# result_df\n",
    "result = pd.merge(a,b,left_index=True,right_index=True)\n",
    "result.columns=['user_id','merchant_id','prob']\n",
    "result\n",
    "result.to_csv('C:\\\\Users\\\\22311\\\\Desktop\\\\longdan2.csv',index=False)"
   ],
   "id": "172b42db3a9b7aa8",
   "outputs": [],
   "execution_count": 39
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
